﻿using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using ApprovalFlow.Interface;

namespace ApprovalFlow.sample1
{
    class Program
    {
        static void Main(string[] args)
        {
            IServiceProvider serviceProvider = ConfigureServices();

            //start the workflow host
            var host = serviceProvider.GetService<IWorkflowHost>();
            var subscriptionRepository = serviceProvider.GetService<ISubscriptionRepository>();
            var workflowRepository = serviceProvider.GetService<IWorkflowRepository>();
            var eventRepository = serviceProvider.GetService<IEventRepository>();
            var userProvider = (UserProvider)serviceProvider.GetService<IUserProvider>();


            host.Start();

            //开启一个新的审批
            string instance_id = host.StartWorkflow("PROC-EF6YJL35P2xxxx", "data", "reference").Result;
            var instance = workflowRepository.GetWorkflowInstance(instance_id).Result;

            writeinfo(instance);
            void writeinfo(ApprovalFlow.Models.WorkflowInstance instance)
            {
                Console.WriteLine($"====================实例状态[{instance.Status}]====================");

                foreach (var pointer in instance.ExecutionPointers)
                {
                    Console.WriteLine($"{ pointer.StepName} - {pointer.Status}");
                }
                Console.WriteLine("====================等待操作====================");
            }


            var subs = subscriptionRepository.GetSubscriptions(instance_id).Result;
            do
            {
                // 同意审批
                var sub = subs.FirstOrDefault();
                var value = Console.ReadLine();
                IApprovalExecutor runner = serviceProvider.GetService<IApprovalExecutor>();
                switch (value)
                {
                    case "y":
                        Console.WriteLine($"[{sub.EventName}] - 用户发送审批请求：\"{value}\"----------，请输入审批用户");
                        userProvider.UserID = Console.ReadLine();
                        runner.Agree(sub.EventKey, "无").Wait();
                        break;
                    case "n":
                        Console.WriteLine($"[{sub.EventName}] - 用户发送审批请求：\"{value}\"----------，请输入审批用户");
                        runner.Refuse(sub.EventKey, "无").Wait();
                        break;
                    case "t":
                        runner.TerminateWorkflow(instance_id).Wait();
                        break;
                    case "i":
                        writeinfo(instance);
                        break;
                    case "c":
                        Console.Clear();
                        break;
                }
                subs = subscriptionRepository.GetSubscriptions(instance_id).Result;
            } while (subs.Any());

            Console.WriteLine("====================生成的流程如下====================");
            var evt = eventRepository.GetEvent("");
            foreach (var pointer in instance.ExecutionPointers)
            {
                Console.WriteLine($"{ pointer.StepName} - {pointer.Status}");
            }
            Console.WriteLine("====================等待进一步操作====================");
            Console.ReadLine();
        }

        private static IServiceProvider ConfigureServices()
        {
            //setup dependency injection
            IServiceCollection services = new ServiceCollection();
            services.AddLogging();
            services.AddWorkflow(x =>
            {
                x.UseSqlServer("", true, true);
            });
            services.AddSingleton<IUserProvider, UserProvider>();
            var serviceProvider = services.BuildServiceProvider();
            return serviceProvider;
        }
    }
}
